package cn.cdnsu.auth.service.impl;

import cn.cdnsu.auth.dto.LoginDTO;
import cn.cdnsu.auth.service.ILoginService;
import cn.cdnsu.base.util.LoginContext;
import cn.cdnsu.org.domain.Employee;
import cn.cdnsu.org.mapper.EmployeeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.UUID;

@Service
public class LoginServiceImpl implements ILoginService {

    @Autowired
    private EmployeeMapper mapper;
    @Override
    public Map<String, Object> login(LoginDTO user) {
        if (StringUtils.isEmpty(user.getUsername())||StringUtils.isEmpty(user.getPassword())){
            throw new RuntimeException("请输入账号和密码");
        }
        //根据账号查对象
        Employee employee1 = mapper.selectByName(user.getUsername());
        if (Objects.isNull(employee1)){
            throw new RuntimeException("账号或密码错误");
        }
        if (!user.getPassword().equals(employee1.getPassword())){
            throw new RuntimeException("账号或密码错误");
        }
        String uuid= UUID.randomUUID().toString();
        LoginContext.loginMap.put(uuid,employee1);
        HashMap<String, Object> map = new HashMap<>();
        map.put("tokenYa",uuid);
        employee1.setPassword(null);
//        employee1.setId(null);
        map.put("user",employee1);

        return map;
    }
}
